package web.common;

import org.dom4j.*;
import org.dom4j.io.SAXReader;

import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: duanbin
 * Date: 2007-8-21
 * Time: 22:40:37
 * To change this template use File | Settings | File Templates.
 */
public class CodeEngineConfig {
    protected static Document document;
    public static final String CODE_ENGINE_CONFIG_FILE = "D:\\code_engine\\classes\\code-engine-config.xml";
    public static final String CONTROLLER_FILE_SUFFIX = "Controller.java";
    public static final String LAYOUT_COMPONENT_TILES = "tiles";
    public static final String LAYOUT_COMPONENT_SITEMESH = "sitemesh";

    static {
        document = XmlAccessUtil.getXmlDoc(CODE_ENGINE_CONFIG_FILE);
    }

    public static String getTemplateDir() {
        return XmlAccessUtil.getPath(document.selectSingleNode("//templateDir").getText());
    }

    public static String getBasePackage() {
        Node node = document.selectSingleNode("//package/@base");
        return node.getText();
    }

    public static String getAppPackage() {
        Node node = document.selectSingleNode("//package/@app");
        return getBasePackage() + "." + node.getText();
    }

    public static String getDistDir() {
        return XmlAccessUtil.getPath(document.selectSingleNode("//distDir").getText());
    }

    public static String getControllerPackage() {
        Node node = document.selectSingleNode("//package/controller");
        return getAppPackage() + "." + node.getText();
    }

    public static String getManagerPackage() {
        Node node = document.selectSingleNode("//package/manager");
        return getAppPackage() + "." + node.getText();
    }

    public static List<String> getControllerNames() {
        List nodes = document.selectNodes("//controller/@name");
        List<String> names = new ArrayList<String>();
         for (Object aList : nodes) {
                Attribute attribute = (Attribute) aList;
                names.add(attribute.getValue());
            }
        return names;
    }

    public static boolean generateSpringBeanDef(String name) {
        Node node1 = document.selectSingleNode("//controller[@name='"+name+"']/@springBeanDef");
        return node1 != null && Boolean.valueOf(node1.getText());
    }

    public static String getControllerExtends(String name) {  //optional
        Node node1 = document.selectSingleNode("//controller[@name='"+name+"']/@extends");
        if(node1 != null){
            return node1.getText();
        }
        return null;
    }

    public static List<String> getControllerManagers(String name) {
        return XmlAccessUtil.getTokenValues(document, "//controller[@name='"+name+"']/managers");
    }

    public static String getFisrtControllerManager(String name) {
        List<String> list = getControllerManagers(name);
        return list!=null?list.get(0):null;
    }

    public static List getControllerServices(String name) {
        return XmlAccessUtil.getTokenValues(document, "//controller[@name='"+name+"']/services");
    }

    public static String getControllerTemplate(String name) {
        Node node1 = document.selectSingleNode("//controller[@name='"+name+"']/@template");
        return node1.getText();
    }

    public static String getSpringBaseDir() {
        return XmlAccessUtil.getPath(document.selectSingleNode("//spring/@baseDir").getText());
    }

    public static String getSpringLayoutComponent() {
        Node node1 = document.selectSingleNode("//spring/@layoutComponent");
        return node1.getText();
    }

    public static String getSpringManagerFile() {
        Node node1 = document.selectSingleNode("//spring/manager");
        return getSpringBaseDir() + node1.getText();
    }

    public static String getSpringServletFile() {
        Node node1 = document.selectSingleNode("//spring/servlet");
        return getSpringBaseDir() + node1.getText();
    }

    public static String getSpringSecurityFile() {
        Node node1 = document.selectSingleNode("//spring/security");
        return getSpringBaseDir() + node1.getText();
    }

    public static String getTilesFile() {
        Node node1 = document.selectSingleNode("//spring/tiles");
        return getSpringBaseDir() + node1.getText();
    }

    /**
     * 利用XPath操作XML文件，打印指定节点或者属性的值, using xpath 1.0
     *
     * @param filename String 待操作的XML文件（相对路径或者绝对路径）
     */
    public void printSelectedNodeValue(String filename) {
        try {
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(new File(filename));
            List list;

//            XPath xPath = document.createXPath("/students");
//            Object ret = xPath.selectNodes("//qn1:college/@leader[.!='leader1']");
//            System.out.println("ret:  " + ret);

//            list = document.selectNodes("for $line in /po:PurchaseOrder/po:OrderLines/po:Line\n" +
//                    "   return $line/po:Price * $line/po:Quantity");
//            for (Object aList : list) {
//                Attribute attribute = (Attribute) aList;
//                System.out.println("//qn1:college/@leader:  " + attribute.getValue());
//            }

            list = document.selectNodes("//qn1:college/@leader[.!='leader1']");
            for (Iterator it = list.iterator(); it.hasNext();) {
                Object aList = (Object) it.next();
                Attribute attribute = (Attribute) aList;
                System.out.println("//qn1:college/@leader:  " + attribute.getValue());
            }

            //打印所有student节点的属性age值，如果有的话
//            list = document.selectNodes("/students/student/@age");
//            for (Object aList : list) {
//                Attribute attribute = (Attribute) aList;
//                System.out.println("/students/student/@age：" + attribute.getValue());
//            }

            //打印所有college节点值，如果有的话
//            list = document.selectNodes("/students/student");
//            for (Object aList1 : list) {
//                Element bookElement = (Element) aList1;
//                Iterator iterator = bookElement.elementIterator("college");
//                while (iterator.hasNext()) {
//                    Element titleElement = (Element) iterator.next();
//                    System.out.println("/students/student/college：" + titleElement.getText());
//                }
//            }

            //测试节点的一些方法
//            list = document.selectNodes("//city");
//            for (Object aList2 : list) {
//                Element titleElement = (Element) aList2;
//                System.out.print("//telephone：getName:" + titleElement.getName());
//                System.out.print("  ##getNodeType:" + titleElement.getNodeType());
//                System.out.print("  ##getTextTrim:" + titleElement.getTextTrim());
//                System.out.print("  ##getNamespaceURI:" + titleElement.getNamespaceURI());
//                System.out.print("  ##getNodeTypeName:" + titleElement.getNodeTypeName());
//                System.out.print("  ##getQualifiedName:" + titleElement.getQualifiedName());
//                System.out.print("  ##getUniquePath:" + titleElement.getUniquePath());
//                System.out.println("  ##getPath:" + titleElement.getPath());
//            }

            //打印所有name节点值，如果有的话，与上面college的取法不一样
//            list = document.selectNodes("/students/student/name");
//            Iterator iter = list.iterator();
//            for (Object aList3 : list) {
//                Element titleElement = (Element) aList3;
//                System.out.println("/students/student/name：" + titleElement.getText());
//            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
